package com.cgw.game.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.cgw.game.R;

public class SleepProgressView extends View {

    private Context context;
    private int width;
    private int height;
    private Paint paint;
    private Bitmap fallbit;
    private Bitmap awakebit;
    private Paint pointPaint;
    private Paint pLine;
    private float radius = 350f;
    private float bgWidth = 35f;
    private String bottomText = "2019.03.20";
    private String centerText = "Great";
    private int[] time = new int[4];
    public SleepProgressView(Context context) {
        this(context, null);
    }

    public SleepProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SleepProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        time[0] = 23;
        time[1] = 7;
        time[2] = 13;
        time[3] = 15;
        init();
    }


    private void init() {

        Resources resources = context.getResources();
        fallbit = BitmapFactory.decodeResource(resources, R.mipmap.fallsleep);
        awakebit = BitmapFactory.decodeResource(resources, R.mipmap.awake);
        paint = new Paint();
        paint.setColor(resources.getColor(R.color.sleep_bg));
        paint.setAntiAlias(true);
        paint.setStrokeWidth(bgWidth);
        paint.setStyle(Paint.Style.STROKE);

        pointPaint = new Paint();
        pointPaint.setColor(resources.getColor(R.color.sleep_bg));
        pointPaint.setAntiAlias(true);
        pointPaint.setStyle(Paint.Style.FILL);

        pLine = new Paint();
        pLine.setStrokeWidth(6);
        pLine.setColor(resources.getColor(R.color.sleep_bg));
        pLine.setAntiAlias(true);
        pLine.setStyle(Paint.Style.FILL);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = measureSize(widthMeasureSpec);
        height = measureSize(heightMeasureSpec);
        setMeasuredDimension(width, height);
        radius = width>height?height/2:width/2;
        radius =  radius - DensityUtil.dpToPx(14);
    }

    private int measureSize(int measureSpec) {
        int length;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if (mode == MeasureSpec.EXACTLY) {
            length = size;
        } else {
            length = DensityUtil.dip2px(context, 300);
            if (mode == MeasureSpec.AT_MOST) {
                length = Math.min(length, size);
            }
        }
        return length;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(width / 2, height / 2);
        drawBgCircle(canvas);
        drawPoint(canvas);
        //drawBitmap(canvas);
        drawText(canvas);
        drawProgress(canvas);
    }

    /**
     * 头部title点击区域
     */
    private RectF rectF;

    private void drawText(Canvas canvas) {
        pLine.setColor(context.getResources().getColor(R.color.sleep_bg_text));
        String time = "12:00";
        pLine.setTextSize(DensityUtil.dpToPx(14));
        int textWidthTime = (int) pLine.measureText(time);
        canvas.drawText(time, -textWidthTime / 2, -radius, pLine);
        String time1 = "00:00";

        int textWidthTime1 = (int) pLine.measureText(time1);
        canvas.drawText(time1, -textWidthTime1 / 2, radius+DensityUtil.dpToPx(10), pLine);
        String time2 = "06:00";

        int textWidthTime2 = (int) pLine.measureText(time2);
        canvas.drawText(time2, -textWidthTime2  - radius, 10, pLine);
        String time3 = "18:00";

        canvas.drawText(time3, radius, 10, pLine);

        String title = "睡眠评级";
        Rect rect = new Rect();
        pLine.getTextBounds(title, 0, title.length(), rect);
        float y = (-(radius-DensityUtil.dpToPx(14)) + bgWidth + DensityUtil.dpToPx(20)) / 2 - 30;
        pLine.setColor(context.getResources().getColor(R.color.sleep_text));
        canvas.drawText(title, -rect.width() / 2, y, pLine);
        pLine.setStyle(Paint.Style.STROKE);
        pLine.setColor(context.getResources().getColor(R.color.sleep_center_text));
        pLine.setStrokeWidth(2);
        canvas.drawRoundRect((-rect.width() / 2 - DensityUtil.dpToPx(20)),
                y - rect.height() - DensityUtil.dpToPx(8),
                (rect.width() / 2 + DensityUtil.dpToPx(20)),
                y + DensityUtil.dpToPx(10)
                , 50, 50
                , pLine);
        rectF = new RectF((-rect.width() / 2 - DensityUtil.dpToPx(10)),
                y - rect.height() - DensityUtil.dpToPx(8),
                (rect.width() / 2 + DensityUtil.dpToPx(10)),
                y + DensityUtil.dpToPx(10));

        pLine.setTextSize(DensityUtil.dpToPx(14));
        pLine.setColor(context.getResources().getColor(R.color.sleep_text));
        float bottomTextWidth = pLine.measureText(bottomText);
        pLine.setStyle(Paint.Style.FILL);
        canvas.drawText(bottomText, -bottomTextWidth / 2,(radius-DensityUtil.dpToPx(14))*2/3, pLine);

        pLine.setTextSize(DensityUtil.dpToPx(30));
        Rect rect1 = new Rect();
        pLine.getTextBounds(centerText, 0, centerText.length(), rect1);
        int h = rect1.height();
        int w = rect1.width();
        pLine.setColor(context.getResources().getColor(R.color.sleep_center_text));
        pLine.setStyle(Paint.Style.FILL);
        canvas.drawText(centerText, -w / 2, h / 2, pLine);
    }


    private int start = 23;
    private int end = 7;
    private int angle = 360 / 24;
    /**
     * 最大刻度
     */
    private double maxScale = 24;

    private void drawProgress(Canvas canvas) {
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(context.getResources().getColor(R.color.sleep_center_text));
        paint.setStrokeCap(Paint.Cap.BUTT);
        paint.setStrokeWidth(15);
        canvas.rotate(90f);
        canvas.save();
        Log.i("progress===", (float) start * angle + "");
        Log.i("progress====", range(start, end) * angle + "");

        for (int i = 0; i < time.length; i++) {
            start = time[i];
            end = time[i+1];
            canvas.drawArc(-radius+DensityUtil.dpToPx(14),
                    -radius+DensityUtil.dpToPx(14),
                    radius-DensityUtil.dpToPx(14),
                    radius-DensityUtil.dpToPx(14),
                    start * angle, range(start, end) * angle, false, paint);

            i++;
        }
        canvas.restore();

    }

    private int range(int start, int end) {
        if (start > end) {
            return (int) (maxScale - start) + end;
        }
        return end - start;
    }


    private void drawBgCircle(Canvas canvas) {
        paint.setColor(context.getResources().getColor(R.color.sleep_bg));
        paint.setStrokeWidth(bgWidth);
        canvas.drawCircle(0, 0, radius-DensityUtil.dpToPx(14), paint);
    }


    private void drawPoint(Canvas canvas) {
        pLine.setColor(context.getResources().getColor(R.color.sleep_bg));
        pLine.setStyle(Paint.Style.FILL);
        canvas.save();
        canvas.rotate(-90f);
        double angel = 360 / 24;
        for (float i = 0; i < 24; i++) {
            if (i % 6 == 0) {
                pLine.setStrokeWidth(10);
                canvas.drawLine(
                        (float) ((radius-DensityUtil.dpToPx(14))  * Math.cos((angel * i) * Math.PI / 180)),
                        (float) ((radius-DensityUtil.dpToPx(14))  * Math.sin((angel * i) * Math.PI / 180)),
                        (float) ((radius-DensityUtil.dpToPx(14)-bgWidth*3/2) * Math.cos((angel * i) * Math.PI / 180)),
                        (float) ((radius-DensityUtil.dpToPx(14)-bgWidth*3/2) * Math.sin((angel * i) * Math.PI / 180)), pLine);

            }else if (i % 2 == 0) {
                pLine.setStrokeWidth(6);
                canvas.drawLine(
                        (float) ((radius-DensityUtil.dpToPx(14))  * Math.cos((angel * i) * Math.PI / 180)),
                        (float) ((radius-DensityUtil.dpToPx(14))  * Math.sin((angel * i) * Math.PI / 180)),
                        (float) ((radius-DensityUtil.dpToPx(14)-bgWidth*3/2) * Math.cos((angel * i) * Math.PI / 180)),
                        (float) ((radius-DensityUtil.dpToPx(14)-bgWidth*3/2) * Math.sin((angel * i) * Math.PI / 180)), pLine);

            }else {
                pLine.setStrokeWidth(6);
                canvas.drawLine(
                        (float) ((radius-DensityUtil.dpToPx(14)) * Math.cos((angel * i) * Math.PI / 180)),
                        (float) ((radius-DensityUtil.dpToPx(14))  * Math.sin((angel * i) * Math.PI / 180)),
                        (float) ((radius-DensityUtil.dpToPx(14)-bgWidth) * Math.cos((angel * i) * Math.PI / 180)),
                        (float) ((radius-DensityUtil.dpToPx(14)-bgWidth) * Math.sin((angel * i) * Math.PI / 180)), pLine);
            }

        }
        canvas.restore();
        canvas.save();
    }


    private int getRang(int value) {
        if (value < 6) {
            return 24 + (value - 6);
        }
        return value - 6;
    }


    public void setTimeAndText(int[] time,String bottomText,String centerText) {
        this.time = time;
        this.bottomText = bottomText;
        this.centerText = centerText;
        invalidate();
    }

}
